package ms.maplestory.boot.packets;

import ms.maplestory.boot.client.character.Char;
import ms.maplestory.boot.connection.OutPacket;
import ms.maplestory.boot.connection.opcode.OutHeader;
import ms.maplestory.boot.packets.universal.CharacterData;
import ms.maplestory.boot.enums.stage.CharDataFlag;
import ms.maplestory.boot.util.FileTime;
import ms.maplestory.boot.world.field.Field;


public class StagePacket {

    public static OutPacket sendSetField(Char chr, boolean characterData, Field field, int portal) {
        OutPacket out = new OutPacket(OutHeader.LP_SetField);
        out.encodeShort(0); // ClientOptMan
        out.encodeInt(chr.getClient().getChannelId() - 1);
        out.encodeInt(0); // OldDriverID
        out.encodeByte(1); // NotifierMessage
        out.encodeByte(characterData);
        out.encodeShort(0); // Notifier
        if (characterData) {
            chr.getCalcDamage().encode(out);
            CharacterData.encode(out, chr, CharDataFlag.All);
            out.encodeInt(0); // LogoutGiftConfig
            for (int i = 0; i < 3; i++) {
                out.encodeInt(0);
            }
        } else {
            out.encodeByte(0);
            out.encodeInt(field.getId());
            out.encodeByte(portal);
            out.encodeInt(chr.getStat().getHp());
            boolean pos = false;
            out.encodeByte(pos);
            if (pos) {
                out.encodeInt(chr.getPosition().getX());
                out.encodeInt(chr.getPosition().getY());
            }
        }
        out.encodeFT(FileTime.currentTime());
        return out;
    }

}
